<?php

namespace App\Http\Controllers;

use App\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Post;
use App\Comment;
use App\Zan;

class PostController extends Controller
{
    //文章列表
    public function index()
    {
        $posts = Post::withCount('comments', 'zans')->orderBy('created_at', 'desc')->paginate(10);
        //预加载
        $posts->load('user');
        $banners = json_decode(file_get_contents('banners.php'), true);
        return view("post/index", compact('posts', 'banners'));
    }

    public function search()
    {
        $this->validate(request(), [
            'query' => 'required'
        ]);

        $query = request('query');
        $posts = Post::search($query)->paginate(2);

        return view("post/search", compact('posts', 'query'));
    }

    //文章详情
    public function show(Post $post)
    {
        //预加载
        $post->load('comments', 'user');

        return view("post/show", compact('post'));
    }

    //文章评论行为
    public function comment(Post $post)
    {

        $this->validate(request(), [
            'content' => 'required|min:3',
        ]);

        //new一个留言对象

        $comment = new Comment();
        $comment->user_id = Auth::id();
        $comment->content = trim(request('content'));

        //关联模型保存对象
        $post->comments()->save($comment);

        return back();
    }

    //文章点赞行为
    public function zan(Post $post)
    {
        $params = [
            'user_id' => Auth::id(),
            'post_id' => $post->id,
        ];

        Zan::firstOrCreate($params);

        return back();
    }

    //文章取消赞行为
    public function unzan(Post $post)
    {
        $post->zan(Auth::id())->delete();

        return back();
    }

    //创建文章
    public function create()
    {
        return view("post/create");
    }

    //创建文章行为
    public function store()
    {
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ]);

        $user_id = Auth::id();
        $params = array_merge(request(['title', 'content']), compact('user_id'));
        $post = Post::create($params);

        return redirect('/posts');
    }

    //编辑文章
    public function edit(Post $post)
    {

        return view("post/edit", compact('post'));
    }

    //编辑文章行为
    public function update(Post $post)
    {
        $this->validate(request(), [
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10',
        ]);

        //权限判断
        $this->authorize('update', $post);

        $post->title = request('title');
        $post->content = request('content');

        $post->save();

        return redirect("/posts/{$post->id}");
    }

    //文章图片上传
    public function imageUpload(Request $request)
    {
        //从请求里拿出wangEditorH5File然后储存public 空间并重命名
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));
        //返回一个路径+文件名
        return asset('storage/' . $path);
    }

    //删除文章
    public function delete(Post $post)
    {

        //权限判断
        $this->authorize('delete', $post);

        $post->delete();

        return redirect('/posts');
    }
}
